package com.kcs.fxmargin.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.kcs.common.utils.Constant;
import com.kcs.common.utils.commonUtils;
import com.kcs.fxmargin.bo.UserProFileBO;
import com.kcs.fxmargin.model.MUsers;

public class UsersDao {
	private Logger log = Logger.getLogger(Constant.LOG_FX);
	
	public List<UserProFileBO> searchUser(Connection conn, PreparedStatement pstm, ResultSet rs, UserProFileBO bo) throws Exception{
		StringBuffer sql 					= new StringBuffer();
		List<UserProFileBO> userProFileList	= new ArrayList<UserProFileBO>();
		UserProFileBO userProFile			= null;
		
		sql.append(" select u.id, u.user_id, u.user_name, r.name role_name, r.id role_id, u.row_version ");
		sql.append(" from m_users u left join m_role r on u.role_id = r.id ");
		sql.append(" where u.active_status = 'A' ");
		sql.append(" and r.active_status = 'A' ");
		
		if(bo.getUserId() != null && !"".equals(bo.getUserId())) 		sql.append(" and u.user_id = ? ");
		if(bo.getUserName() != null && !"".equals(bo.getUserName())) 	sql.append(" and UPPER(u.user_name) like UPPER(?) ");
		if(bo.getRoleId() != null && !"".equals(bo.getRoleId())) 		sql.append(" and u.role_id = ? ");

		sql.append(" order by user_id ");
		
		log.info("SQL -> " + sql.toString());

		int index = 1;
		pstm = conn.prepareStatement(sql.toString());
		
		if(bo.getUserId() != null && !"".equals(bo.getUserId())) {
			pstm.setString(index, bo.getUserId());
			index++;
		}
		
		if(bo.getUserName() != null && !"".equals(bo.getUserName())) {
			pstm.setString(index, commonUtils.like(bo.getUserName()));
			index++;
		}
		
		if(bo.getRoleId() != null && !"".equals(bo.getRoleId())) {
			pstm.setString(index, bo.getRoleId());
			index++;
		}
		
		rs = pstm.executeQuery();
		index = 1;
		while(rs.next()){
			userProFile = new UserProFileBO();
			userProFile.setRowNum(index);
			userProFile.setId(rs.getString("id"));
			userProFile.setUserId(rs.getString("user_id"));
			userProFile.setUserName(rs.getString("user_name"));
			userProFile.setRoleId(rs.getString("role_id"));
			userProFile.setRoleName(rs.getString("role_name"));
			userProFile.setRowVersion(rs.getInt("row_version"));
			userProFile.setJsonObject();
			userProFileList.add(userProFile);
			
			index++;
			
		}
		
		log.info("userProFileList size = " + userProFileList.size());
		
		return userProFileList;
	}

	public String insertUser(Connection conn, PreparedStatement pstm, MUsers users) throws Exception{
		
		StringBuffer sql 		= new StringBuffer();
		
		int insert = 0;
		
		sql.append(" insert into m_users ");
		sql.append(" (id, user_id, user_name, role_id, active_status, create_by, create_dt, update_by, update_dt, row_version, via_ldap) ");
		sql.append(" values (?,?,?,?,?,?,getDate(),?,getDate(), ?, 1) ");
		log.info("SQL -> " + sql.toString());
		
		pstm = conn.prepareStatement(sql.toString());
		pstm.setString(1, users.getId());
		pstm.setString(2, users.getUserId());
		pstm.setString(3, users.getUserName());
		pstm.setString(4, users.getRoleId());
		pstm.setString(5, users.getActiveStatus());
		pstm.setString(6, users.getCreateby());
		pstm.setString(7, users.getUpdateBy());
		pstm.setInt(8, 1);
		
		insert = pstm.executeUpdate();
		
		log.info("insert = " + insert);
		
		return insert == 1 ? "s" : "e";
		
	}
	
	public String updateUser(Connection conn, PreparedStatement pstm, MUsers users) throws Exception{
		
		StringBuffer sql 		= new StringBuffer();
		
		int update = 0;
		
		sql.append(" update m_users ");
		sql.append(" set user_id = ? , ");
		sql.append(" user_name = ? , ");
		sql.append(" role_id = ? , ");
		sql.append(" update_by = ? , ");
		sql.append(" update_dt = getDate() , ");
		sql.append(" row_version = ? ");
		sql.append(" where id = ? ");

		log.info("SQL -> " + sql.toString());
		
		pstm = conn.prepareStatement(sql.toString());
		pstm.setString(1, users.getUserId());
		pstm.setString(2, users.getUserName());
		pstm.setString(3, users.getRoleId());
		pstm.setString(4, users.getUpdateBy());
		pstm.setInt(5, users.getRowVersion());
		pstm.setString(6, users.getId());
		
		update = pstm.executeUpdate();
		
		log.info("update = " + update);
		
		return update == 1 ? "s" : "e";
	}
	
	public String deleteUser(Connection conn, PreparedStatement pstm, String id) throws Exception{
		
		StringBuffer sql 		= new StringBuffer();
		
		int delete = 0;

		//sql.append(" delete from m_users where id = ? ");
		sql.append("update m_users set active_status = 'D' where id = ?");
		
		log.info("SQL -> " + sql.toString());
		
		pstm = conn.prepareStatement(sql.toString());
		pstm.setString(1, id);
		
		delete = pstm.executeUpdate();
		
		log.info("delete = " + delete);
		
		return delete == 1 ? "s" : "e";
	}
	
	public String loginUpdateUser(Connection conn, PreparedStatement pstm, String id, String sessionId) throws Exception{
		
		StringBuffer sql 		= new StringBuffer();
		
		int update = 0;
		
		sql.append(" update m_users ");
		sql.append(" set last_access_dt = getDate(), ");
		sql.append(" session_id = '" + sessionId + "' ");
		sql.append(" where  active_status = 'A' ");
		sql.append(" and user_id = '" + id + "' ");

		log.info("SQL -> " + sql.toString());
		
		pstm = conn.prepareStatement(sql.toString());
//		pstm.setString(1, id);
//		pstm.setString(2, sessionId);
		
		update = pstm.executeUpdate();
		
		log.info("update = " + update);
		
		return update == 1 ? "s" : "e";
	}
	
	public String getSessionId(Connection conn, PreparedStatement pstm, ResultSet rs, String id) throws Exception {
		StringBuffer sql 		= new StringBuffer();
		String sessionId		= "";
		
		sql.append(" select session_id from m_users where user_id = ? ");
		
		pstm = conn.prepareStatement(sql.toString());
		pstm.setString(1, id);
		
		rs = pstm.executeQuery();
		
		while(rs.next()){
			sessionId = rs.getString("session_id");
		}
		
		return sessionId;
	}
}
